package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwcqT;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.crm.entity.GwcszT;
import cn.e4j.modular.crm.mapper.GwcqTMapper;
import cn.e4j.modular.crm.mapper.GwcsTMapper;
import cn.e4j.modular.crm.service.GwcsTService;
import cn.e4j.modular.erp.mapper.E10Mapper;
import cn.e4j.modular.erp.mapper.GPMapper;
import cn.e4j.modular.erp.mapper.T100Mapper;
import cn.e4j.modular.erp.mapper.YFMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.math.BigDecimal.ROUND_HALF_DOWN;
import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxc
 * @since 2019-01-18
 */
@Service
public class GwcsTServiceImpl extends ServiceImpl<GwcsTMapper, GwcsT> implements GwcsTService {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Autowired
    private GwcqTMapper gwcqTMapper;
    @Autowired
    private YFMapper yfMapper;
    @Autowired
    public E10Mapper e10Mapper;
    @Autowired
    public GPMapper gpMapper;
    @Autowired
    public T100Mapper t100Mapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

//        List<Map<String, Object>> gwcsDtos = this.baseMapper.listPage(page, dto);
        List<Map<String, Object>> gwcsDtos = InvokeUtil.invokeBeanMethod("gwcsTMapper,listPage",page,dto);

        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwcs(Dto dto) {

//        List<Map<String, Object>> gwcsDtos = this.baseMapper.listGwcs(dto);
        List<Map<String, Object>> gwcsDtos = InvokeUtil.invokeBeanMethod("gwcsTMapper,listGwcs",dto);
        for (Map<String, Object> gwcs : gwcsDtos) {
            if (gwcs.containsKey("GWCS109") && !gwcs.get("GWCS109").toString().isEmpty()) {
                dto.put("GWCS109", gwcs.get("GWCS109").toString());
                String GWDC003 = this.baseMapper.getGwdc003(dto);
                gwcs.put("GWDC003", GWDC003);
            }
        }

        return gwcsDtos;
    }

    @Transactional
    @Override
    public void delete(GwcsT gwcs) {
        UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
        gwcsTUpdateWrapper.eq("GWCSENT", gwcs.getGWCSENT());
        gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcs.getGWCSDOCNO());
        GwcqT gwcqT = this.baseMapper.getByGwcqdocno(gwcs.getGWCSENT(), gwcs.getGWCS043());
        if (gwcqT != null) {
            UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
            gwcqTUpdateWrapper.eq("GWCQENT", gwcs.getGWCSENT());
            gwcqTUpdateWrapper.eq("GWCQDOCNO", gwcs.getGWCS043());
            gwcqT.setGWCQACTI("Y");
            gwcqT.update(gwcqTUpdateWrapper);
            gwcqT.delete(gwcqTUpdateWrapper);
        }
        gwcs.delete(gwcsTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwcsT> gwcsTList) {
        for (GwcsT gwcsT : gwcsTList) {
            UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
            gwcsTUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
            gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
            GwcqT gwcqT = this.baseMapper.getByGwcqdocno(gwcsT.getGWCSENT(), gwcsT.getGWCS043());
            if (gwcqT != null) {
//                UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
//                gwcqTUpdateWrapper.eq("GWCQENT", gwcsT.getGWCSENT());
//                gwcqTUpdateWrapper.eq("GWCQDOCNO", gwcsT.getGWCS043());
//                gwcqT.update(gwcqTUpdateWrapper);
                this.baseMapper.updateGwcq(gwcqT);
            }
            GwcszT gwcszT = new GwcszT();
            UpdateWrapper<GwcszT> gwcszTUpdateWrapper = new UpdateWrapper<>();
            gwcszTUpdateWrapper.eq("GWCSZENT", gwcsT.getGWCSENT());
            gwcszTUpdateWrapper.eq("GWCSZDOCNO", gwcsT.getGWCSDOCNO());
            gwcszT.delete(gwcszTUpdateWrapper);
            gwcsT.delete(gwcsTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public Dto getGwcsdocno(Dto dto) {
        Dto pDto = Dtos.newDto();
        Date date = new Date();
        pDto.put("GWCQDOCNO", "ZS" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        pDto.put("GWCQDOCDATE", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        ShiroUser user = ShiroKit.getUser();
        if (user.getTYPE().equals("4")) {
            Dto sDto = Dtos.newDto();
            sDto.put("ENT", user.getDBC());
            sDto.put("LANG", LangUtils.getLang(user.getLANG()));
            sDto.put("GWAA001", user.getACCOUNT());
            Dto gwaa = this.baseMapper.getGwaa(sDto);
            if (gwaa != null) {
                pDto.put("GWCS010", gwaa.getString("GWAA009"));
                pDto.put("GWCS010_NAME", gwaa.getString("GWAKL004"));
            }else{
                pDto.put("GWCS010", "");
                pDto.put("GWCS010_NAME", "");
            }
        }
        pDto.put("GWCS014", user.getID());
        pDto.put("GWCS014_NAME", user.getNAME());
        return pDto;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwam(Page<Object> page, Dto dto) {

//        List<Map<String, Object>> gwcsDtos = this.baseMapper.listGwam(page, dto);
        List<Map<String, Object>> gwcsDtos = InvokeUtil.invokeBeanMethod("gwcsTMapper,listGwam",page,dto);

        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGWCS045(Dto dto) {
        List<Dto> gwcsDtos = this.baseMapper.getGWCS045(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGweaList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gweaDtos = this.baseMapper.listGweaPage(page, dto);
        return gweaDtos;
    }

    @Transactional
    @Override
    public void updateData(List<GwcsT> gwcsTList) {
        for (GwcsT gwcsT : gwcsTList) {
            UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
            gwcsTUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
            gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
            gwcsT.update(gwcsTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwcs032(Dto dto) {
        List<Dto> gwcsDtos = this.baseMapper.listGwcs032(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwcs035(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        Dto resultDto = this.baseMapper.listGwak(shiroUser.getDBC(), inDto.getString("GWAK001"));
        if (resultDto.getString("GWAK056") == "") {
            Dto result = this.baseMapper.listGwaf(shiroUser.getDBC(), inDto.getString("GWAK001"));
            if (result == null) {
                outDto.put("GWCS035", "1");
            } else if (result.getString("GWAF004").isEmpty()) {
                outDto.put("GWCS035", "1");
            } else {
                outDto.put("GWCS035", result.getString("GWAF004"));
            }
        } else {
            outDto.put("GWCS035", resultDto.getString("GWAK056"));
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getUserList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> userDtos = this.baseMapper.listUserPage(page, dto);
        return userDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwcs034(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
//        Dto dto = this.baseMapper.listGwak(shiroUser.getDBC(), inDto.getString("GWAK001"));
        Dto dto = InvokeUtil.invokeBeanMethod("gwcsTMapper,listGwak",shiroUser.getDBC(), inDto.getString("GWAK001"));
        String gwcs033 = dto.getString("GWAK057");
        if(gwcs033 ==null){gwcs033="";}
        if (gwcs033.equals("")) {
            gwcs033 = this.baseMapper.listParams("change_rate_type_");
        }
        String gwac = dto.getString("GWAK008");
        if (gwac.equals("")) {
            gwac = this.baseMapper.listParams("sys_currency_");
        }
        BigDecimal gwcs034 = new BigDecimal(1);
        if (gwcs033.equals("1")) {
            inDto.put("GWEL001", gwac);
            inDto.put("GWEL002", DateUtils.getDate("yyyy-MM-dd"));
            inDto.put("GWELENT", shiroUser.getDBC());
            Dto pdto = this.baseMapper.listGwel(inDto);
            if (pdto != null) {
                gwcs034 = pdto.getBigDecimal("GWEL006");
            }
        } else if (gwcs033.equals("2")) {
            inDto.put("GWEK001", gwac);
            inDto.put("GWEK002", DateUtils.getDate("yyyyMM"));
            inDto.put("GWEKENT", shiroUser.getDBC());
            Dto pdto = this.baseMapper.listGwek(inDto);
            if (pdto != null) {
                gwcs034 = pdto.getBigDecimal("GWEK006");
            }
        }
        Dto outDto = Dtos.newDto();
        outDto.put("GWCS033", gwcs033);//汇率类型
        outDto.put("GWCS034", gwcs034);//汇率
        outDto.put("GWCS006", gwac);//币种
        outDto.put("GWCSPLANT", dto.getString("GWAKPLANT"));//ERP营运中心
        outDto.put("GWCSMLEGAL", dto.getString("GWAKMLEGAL"));//归属法人
        outDto.put("GWCSMLEGAL_NAME", dto.getString("GWAKMLEGAL_NAME"));//归属法人
        outDto.put("GWCS118", dto.getString("GWAK061"));//拆包否
        return outDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwcsInfo(Dto dto) {
        List<Dto> gwcsDtos = this.baseMapper.listGwcsInfo(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwcszInfo(Dto dto) {
        List<Dto> gwcszDtos = this.baseMapper.listGwcszInfo(dto);
        return gwcszDtos;
    }

    @Transactional(readOnly = true)
    public Dto getDBinfo1(String ent, String gwcsmlegal) {
        Dto dbInfo = this.baseMapper.getDBinfo1(ent, gwcsmlegal);
        return dbInfo;
    }

    @Transactional(readOnly = true)
    public Dto getDBinfo2(String biz_code_) {
        Dto dbInfo = this.baseMapper.getDBinfo2(biz_code_);
        return dbInfo;
    }

    @Transactional(readOnly = true)
    public Dto getUDF11(Dto dto) {
        Dto UDF11 = this.baseMapper.getUDF11(dto);
        return UDF11;
    }

    @Transactional(readOnly = true)
    public Dto selectByGwak001(Dto dto) {
        Dto info = this.baseMapper.selectByGwak001(dto);
        return info;
    }

    @Transactional(readOnly = true)
    public Dto getSysOrg(String id) {
        Dto info = this.baseMapper.getSysOrg(id);
        return info;
    }

    @Transactional(readOnly = true)
    public Dto getGwaz(String ent, String gwcsmlegal) {
        Dto gwazDtos = this.baseMapper.getGwaz(ent, gwcsmlegal);
        return gwazDtos;
    }

    @Transactional
    public void addData(List<GwcsT> gwcsTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String plant = gwcsTList.get(0).getGWCSPLANT();
        if (gwcsTList.size() > 0 && !gwcsTList.get(0).getGWCS010().isEmpty() && gwcsTList.get(0).getGWCSPLANT().isEmpty()) {
            Dto pdto = Dtos.newDto();
            pdto.put("GWAK001", gwcsTList.get(0).getGWCS010());
            pdto.put("GWAKENT", shiroUser.getDBC());
            Dto gwakDto = this.baseMapper.getGwakplant(pdto);
            if (gwakDto == null) {
                return;
            }
            plant = gwakDto.getString("GWAKPLANT");
        }
        //增加新的明细
        String gwcstime = DateUtils.formatDate(new Date(), "HH:mm:ss");
        for (int i = 0; i < gwcsTList.size(); i++) {
            GwcsT gwcs = gwcsTList.get(i);
            gwcs.setGWCS041(gwcs.getGWCS040().multiply(gwcs.getGWCS034()));
            gwcs.setGWCS037(gwcs.getGWCS115().multiply(gwcs.getGWCS034()));
            gwcs.setGWCS038(gwcs.getGWCS005());//本币折扣前金额
            gwcs.setGWCS035(gwcs.getGWCS004().divide(gwcs.getGWCS115(),6,ROUND_HALF_DOWN));//折扣率
            gwcs.setGWCSPLANT(plant);
            gwcs.setGWCSSEQ(gwcsTList.get(i).getGWCSSEQ());
            gwcs.setGWCSENT(shiroUser.getDBC());
            gwcs.setGWCSLEGAL(gwcs.getGWCSPLANT());
            gwcs.setGWCSACTI("T");//待提交
            gwcs.setGWCSUSER(shiroUser.getACCOUNT());
            gwcs.setGWCSGRUP(shiroUser.getORG_ID());
            gwcs.setGWCSDATE(LocalDate.now());
            //gwcs.setGWCSTIME(gwcstime);
            gwcs.setGWCSORIG(shiroUser.getORG_ID());
            gwcs.setGWCSORIU(shiroUser.getACCOUNT());
            gwcs.insert();
        }
        UpdateWrapper<GwcsT> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("GWCSDOCNO", gwcsTList.get(0).getGWCSDOCNO());
        GwcsT gwcsT = new GwcsT();
        gwcsT.setGWCSTIME(gwcstime);
        gwcsT.update(updateWrapper);
    }

    @Transactional
    public void editData(List<GwcsT> gwcsTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //删除旧的明细
        Dto deleteDto = Dtos.newDto();
        deleteDto.put("GWCSENT", shiroUser.getDBC());
        deleteDto.put("GWCSDOCNO", gwcsTList.get(0).getGWCSDOCNO());
        this.baseMapper.deleteData(deleteDto);
        //增加新的明细
        String gwcstime = DateUtils.formatDate(new Date(), "HH:mm:ss");
        for (int i = 0; i < gwcsTList.size(); i++) {
            GwcsT gwcs = gwcsTList.get(i);
            gwcs.setGWCS035(gwcs.getGWCS004().divide(gwcs.getGWCS115(),6,ROUND_HALF_UP));//折扣率
            gwcs.setGWCSENT(shiroUser.getDBC());
            gwcs.setGWCSLEGAL(gwcs.getGWCSPLANT());
            gwcs.setGWCSACTI("T");
            gwcs.setGWCSUSER(shiroUser.getACCOUNT());
            gwcs.setGWCSGRUP(shiroUser.getORG_ID());
            gwcs.setGWCSDATE(LocalDate.now());
            gwcs.setGWCSTIME(gwcstime);
            gwcs.setGWCSORIG(shiroUser.getORG_ID());
            gwcs.setGWCSORIU(shiroUser.getACCOUNT());
            gwcs.insert();
        }
    }

    @Override
    @Transactional
    public void insertCOPTD(Dto dto) {
        this.baseMapper.insertCOPTD(dto);
    }

    @Override
    @Transactional
    public int insertCOPTC(Dto dto) {
        int rows = 0;
        rows = this.baseMapper.insertCOPTC(dto);
        return rows;
    }

    @Transactional
    public void deleteCOPTD(Dto dto) {
        this.baseMapper.deleteCOPTD(dto);//插入单头失败时就把所有单身都删除，避免有单身没有单头

    }

    @Override
    @Transactional
    public void update(Dto dto) {
        GwcsT gwcsT = new GwcsT();
        gwcsT.setGWCSACTI(dto.getString("gwcsacti"));
        gwcsT.setGWCS045(dto.getString("gwcs045"));
        gwcsT.setGWCS102(dto.getString("gwcs102"));
        UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
        gwcsTUpdateWrapper.eq("GWCSENT", dto.getString("ent"));
        gwcsTUpdateWrapper.eq("GWCSDOCNO", dto.getString("gwcsdocno"));
        gwcsTUpdateWrapper.eq("GWCSSEQ", dto.getString("gwcsseq"));
        gwcsT.update(gwcsTUpdateWrapper);
    }

    @Transactional
    public void updateGWCSZ(Dto dto) {
        GwcszT gwcszT = new GwcszT();
        gwcszT.setGWCSZDOCNO(dto.getString("gwcsdocno"));
        gwcszT.setGWCSZSEQ(dto.getString("gwcszseq"));
        UpdateWrapper<GwcszT> gwcszTUpdateWrapper = new UpdateWrapper<>();
        gwcszTUpdateWrapper.eq("GWCSZENT", dto.getString("gwcszent"));
        gwcszTUpdateWrapper.eq("GWCSZDOCNO", dto.getString("gwcszdocno"));
        gwcszTUpdateWrapper.eq("GWCSZ003", dto.getString("gwcsz003"));
        gwcszT.update(gwcszTUpdateWrapper);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwcqList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwcqDtos = this.baseMapper.getGwcqPage(page, dto);

        return gwcqDtos;
    }

    @Transactional(readOnly = true)
    public Dto getCOPTC(Dto dto) {
        Dto COPTC = this.baseMapper.getCOPTC(dto);
        return COPTC;
    }

    @Transactional
    @Override
    public void updateGwcs(GwcsT gwcsT) {
        UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
        gwcsTUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
        gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
        gwcsT.update(gwcsTUpdateWrapper);
    }

    @Transactional
    @Override
    public void updateCOPTC(Dto dto) {
        this.baseMapper.updateCOPTC(dto);
        this.baseMapper.updateCOPTD(dto);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwdc001(Page<Object> page, Dto dto) {
        Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
        if (!(gwak == null)) {
            dto.put("GWAK010", gwak.getString("GWAK010"));
        }
        List<Map<String, Object>> gwdcDtos = InvokeUtil.invokeBeanMethod("gwcsTMapper,listGwdc001",page,dto);

        return gwdcDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwda003(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwdaDtos = this.baseMapper.listGwda003(page, dto);

        return gwdaDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getPrice(Dto dto) {
        Dto result = Dtos.newDto();
        List<Dto> list = new ArrayList<>();
        List<GwcsT> gwcs = dto.getReqList(GwcsT.class);
        dto.put("GWCSDOCDATE", gwcs.get(0).getGWCSDOCDATE());
        dto.put("GWCS010", gwcs.get(0).getGWCS010());
        dto.put("GWCS109", gwcs.get(0).getGWCS109());
        dto.put("GWCS118", gwcs.get(0).getGWCS118());
//        Dto gwak = this.baseMapper.getGwak(dto);
        Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
        Dto gwea = Dtos.newDto();
        if (!gwak.getString("GWAK012").isEmpty()) {
            dto.put("GWEA001", gwak.getString("GWAK012"));
            gwea = this.baseMapper.getGwea(dto);
            if (gwea == null) {
                gwea =Dtos.newDto();
                gwea.put("GWEA006", 0);
            }
        } else {
            gwea.put("GWEA006", 0);
        }
        for (GwcsT gwcsT : dto.getReqList(GwcsT.class)) {
            dto.put("GWCS001", gwcsT.getGWCS001());
            dto.put("GWCS010", gwcsT.getGWCS010());
            if (dto.getString("GWCS109").isEmpty()) {
//                result = this.baseMapper.getGwdb016(dto); //获取渠道价
                result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdb016",dto);
                if (result == null) {
                    if (!gwak.getString("GWAK010").isEmpty()) {
                        dto.put("GWCS010", gwak.getString("GWAK010"));
//                        result = this.baseMapper.getGwdb016(dto); //获取渠道价
                        result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdb016",dto);
                        if (result == null) {
                            continue;
                        } else {
                            if (gwak.getString("GWAK058").isEmpty()) {
//                                result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                                Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                                result.put("GWCS113",gwam.getString("GWAM019"));
                            } else {
                                result.put("GWCS113", gwak.getString("GWAK058"));
                            }
                        }
                        dto.put("GWCS113", result.getString("GWCS113"));
//                        result.put("GWCS112", this.baseMapper.getGwas(dto).getString("GWAS003"));
                        Dto gwas=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwas",dto);
                        result.put("GWCS112",gwas.getString("GWAS003"));
                        result.put("GWCS032", gwak.getString("GWAK012"));
                        result.put("GWCS032_", gwea.getBigDecimal("GWEA006"));
                        BigDecimal GWDC_temp = result.getBigDecimal("GWDC024");
                        if (dto.getString("GWCS118").equals("Y")) {
                            BigDecimal temp = new BigDecimal(1).add(gwak.getBigDecimal("GWAK062").divide(new BigDecimal(100)));
                            result.put("GWDC024", result.getBigDecimal("GWDC024").multiply(temp));
                        }
                        result.put("GWCS035", result.getBigDecimal("GWDC024").divide(GWDC_temp,2,ROUND_HALF_DOWN));
                        list.add(result);
                    }
                } else {
                    if (gwak.getString("GWAK058").isEmpty()) {
//                        result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                        Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                        result.put("GWCS113",gwam.getString("GWAM019"));
                    } else {
                        result.put("GWCS113", gwak.getString("GWAK058"));
                    }
                    dto.put("GWCS113", result.getString("GWCS113"));
//                    result.put("GWCS112", this.baseMapper.getGwas(dto).getString("GWAS003"));
                    Dto gwas=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwas",dto);
                    result.put("GWCS112",gwas.getString("GWAS003"));
                    result.put("GWCS032", gwak.getString("GWAK012"));
                    result.put("GWCS032_", gwea.getBigDecimal("GWEA006"));
                    BigDecimal GWDC_temp = result.getBigDecimal("GWDC024");
                    if (dto.getString("GWCS118").equals("Y")) {
                        BigDecimal temp = new BigDecimal(1).add(gwak.getBigDecimal("GWAK062").divide(new BigDecimal(100)));
                        result.put("GWDC024", result.getBigDecimal("GWDC024").multiply(temp));
                    }
                    result.put("GWCS035", result.getBigDecimal("GWDC024").divide(GWDC_temp,2,ROUND_HALF_UP));
                    list.add(result);
                }
            } else {
                Dto gwdc = this.baseMapper.getGwdc004_007(dto);
                //不管该活动是组合还是非组合以及阶梯定价都是可以理解为取gwdc里起订量最小的单价
                if (gwdc.getString("GWDC004").equals("1")) {//组合
                    if (gwdc.getString("GWDC007").equals("2")) {
                        //组合+渠道商
                        if (!gwdc.getString("GWDC005").equals("3")) {
//                            result = this.baseMapper.getGwdc023_zh(dto); //获取活动价
                            result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_zh",dto);
                        } else {
//                            result = this.baseMapper.getGwdc023_3_zh(dto); //获取活动价
                            result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_3_zh",dto);
                        }
                        if (gwak.getString("GWAK058").isEmpty()) {
//                            result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                            Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                            result.put("GWCS113",gwam.getString("GWAM019"));
                        } else {
                            result.put("GWCS113", gwak.getString("GWAK058"));
                        }
                    } else if (gwdc.getString("GWDC007").equals("1")) {
                        //组合+渠道
                        if (!gwak.getString("GWAK010").isEmpty()) {
                            dto.put("GWCS010", gwak.getString("GWAK010"));
                            if (!gwdc.getString("GWDC005").equals("3")) {
//                                result = this.baseMapper.getGwdc023_zh(dto); //获取活动价
                                result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_zh",dto);
                            } else {
//                                result = this.baseMapper.getGwdc023_3_zh(dto); //获取活动价
                                result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_3_zh",dto);
                            }
                            if (gwak.getString("GWAK058").isEmpty()) {
//                                result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                                Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                                result.put("GWCS113",gwam.getString("GWAM019"));
                            } else {
                                result.put("GWCS113", gwak.getString("GWAK058"));
                            }
                        }
                    }
                }
                if (gwdc.getString("GWDC004").equals("2")) {
                    if (gwdc.getString("GWDC007").equals("2")) {
                        //组合+渠道商
                        if (!gwdc.getString("GWDC005").equals("3")) {
//                            result = this.baseMapper.getGwdc023(dto); //获取活动价
                            result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023",dto);
                        } else {
//                            result = this.baseMapper.getGwdc023_3(dto); //获取活动价
                            result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_3",dto);
                        }
                        if (gwak.getString("GWAK058").isEmpty()) {
//                            result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                            Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                            result.put("GWCS113",gwam.getString("GWAM019"));
                        } else {
                            result.put("GWCS113", gwak.getString("GWAK058"));
                        }
                    } else if (gwdc.getString("GWDC007").equals("1")) {
                        //组合+渠道
                        if (!gwak.getString("GWAK010").isEmpty()) {
                            dto.put("GWCS010", gwak.getString("GWAK010"));
                            if (!gwdc.getString("GWDC005").equals("3")) {
//                                result = this.baseMapper.getGwdc023(dto); //获取活动价
                                result =InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023",dto);
                            } else {
//                                result = this.baseMapper.getGwdc023_3(dto); //获取活动价
                                result =InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc023_3",dto);
                            }
                            if (gwak.getString("GWAK058").isEmpty()) {
//                                result.put("GWCS113", this.baseMapper.getGwam(dto).getString("GWAM019"));
                                Dto gwam=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwam",dto);
                                result.put("GWCS113",gwam.getString("GWAM019"));
                            } else {
                                result.put("GWCS113", gwak.getString("GWAK058"));
                            }
                        }
                    }
                }


                dto.put("GWCS113", result.getString("GWCS113"));
//                result.put("GWCS112", this.baseMapper.getGwas(dto).getString("GWAS003"));
                Dto gwas=InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwas",dto);
                result.put("GWCS112",gwas.getString("GWAS003"));
                result.put("GWCS032_", gwea.getBigDecimal("GWEA006"));
                result.put("GWCS032", gwak.getString("GWAK012"));
                result.put("GWCS110", result.getString("GWDC005"));
                result.put("GWDC004", gwdc.getString("GWDC004"));
                if (gwdc.getString("GWDC004").equals("1")) {//组合时起订量以及起订金额
//                    Dto dDto = this.baseMapper.getGwdct(dto); //获取起订金额、起订数量
                    Dto dDto = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdct",dto);
                    if (dDto != null) {
                        result.put("GWDC020", dDto.getBigDecimal("GWDC020"));
                        result.put("GWDC021", dDto.getBigDecimal("GWDC021"));
                    }
                } else {
                    result.put("GWDC020", result.getString("GWDC020"));
                    result.put("GWDC021", result.getString("GWDC021"));
                }

                BigDecimal GWDC_temp = result.getBigDecimal("GWDC024");
                if (dto.getString("GWCS118").equals("Y")) {
                    BigDecimal temp = new BigDecimal(1).add(gwak.getBigDecimal("GWAK062").divide(new BigDecimal(100)));
                    result.put("GWDC024", result.getBigDecimal("GWDC024").multiply(temp));
                }
                result.put("GWCS035", result.getBigDecimal("GWDC024").divide(GWDC_temp,2,ROUND_HALF_UP));
                list.add(result);
            }
        }
        return list;
    }

    @Transactional(readOnly = true)
    public Dto getGwdc020_021(Dto dto) {
        Dto gwdc = Dtos.newDto();
        if (dto.getString("GWDC005").isEmpty()) {
//            gwdc = this.baseMapper.getGwdc(dto);
            gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc",dto);
            if (gwdc == null) {
//                Dto gwak = this.baseMapper.getGwak(dto);
                Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
                if (!(gwak == null)) {
                    dto.put("GWCS010", gwak.getString("GWAK010"));
                }
//                gwdc = this.baseMapper.getGwdc(dto);
                gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc",dto);
            }
            //阶梯定价
            if (gwdc.getString("GWDC022").equals("Y")) {
//                gwdc = this.baseMapper.getGwdc_(dto);
                gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_",dto);
            }
        } else {
//            gwdc = this.baseMapper.getGwdc_tj(dto);
            gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_tj",dto);
            if (gwdc == null) {
//                Dto gwak = this.baseMapper.getGwak(dto);
                Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
                if (!(gwak == null)) {
                    dto.put("GWCS010", gwak.getString("GWAK010"));
                }
//                gwdc = this.baseMapper.getGwdc(dto);
                gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc",dto);
            }
        }

        return gwdc;
    }

    @Transactional(readOnly = true)
    public Dto getGwdc020_021_jieti(Dto dto) {
        Dto rt = Dtos.newDto();
//        Dto gwdc = this.baseMapper.getGwdc_jieti(dto);
        Dto gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_jieti",dto);

        /*if(!gwdc.isEmpty()){
            gwdc = this.baseMapper.getGwdb016(dto);
        }*/

        return gwdc;
    }

    @Transactional
    public void delGwcszByGwcsz007(Dto dto) {
        this.baseMapper.delGwcszByGwcsz007(dto);
    }

    @Transactional(readOnly = true)
    public Dto getGwdd(Dto dto) {
        Dto gwdd = this.baseMapper.getGwdd(dto);
        return gwdd;

    }

    @Transactional(readOnly = true)
    public List<Dto> getGiftInfoData(Dto dto) {
        dto.put("GWCSENT", dto.getString("ENT"));
        //获取赠品信息表gwcsz的数据
        List<Dto> listData = this.baseMapper.getGwcszData(dto);
        //循环赠品信息获取订单总数量/金额、起订数量/金额、最大赠品数量/金额
        for (int i = 0; i < listData.size(); i++) {
            Dto queryDto = dto;
            Dto resultDto = Dtos.newDto();

            queryDto.put("GWCS109", listData.get(i).getString("GWCSZ007"));
            if (listData.get(i).getString("GWDC007").equals("1")) {//渠道
//                Dto gwak = this.baseMapper.getGwak(dto);
                Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
                queryDto.put("GWDC016", gwak.getString("GWAK010"));
            } else {//渠道商
                queryDto.put("GWDC016", dto.getString("GWCS010"));
            }
            if (listData.get(i).getString("GWDC004").equals("1")) {//组合
                if (listData.get(i).getString("GWDC022").equals("Y")) {
                    resultDto = this.baseMapper.getData1_jt(queryDto);
                    if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                        queryDto.put("sum_gwcs005", resultDto.getString("GWCS005_SUM"));
                    }
                    if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                        queryDto.put("sum_gwcs003", resultDto.getString("GWCS003_SUM"));
                    }

//                    Dto gwdct = this.baseMapper.getGwdct(queryDto);
                    Dto gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdct",queryDto);
                    if (gwdct != null) {
                        resultDto.put("GWDC020", gwdct.getString("GWDC020"));
                        resultDto.put("GWDC021", gwdct.getString("GWDC021"));
                        resultDto.put("GWDC025", gwdct.getString("GWDC025"));
                        resultDto.put("GWDC026", gwdct.getString("GWDC026"));
                    }
                } else {
                    resultDto = this.baseMapper.getData1(queryDto);
                }
            } else {//非组合
                queryDto.put("GWCS001", listData.get(i).getString("GWCSZ008"));
                if (listData.get(i).getString("GWDC022").equals("Y")) {
                    resultDto = this.baseMapper.getData2_jt(queryDto);
                    if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                        queryDto.put("sum_gwcs005", resultDto.getString("GWCS005_SUM"));
                    }
                    if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                        queryDto.put("sum_gwcs003", resultDto.getString("GWCS003_SUM"));
                    }

//                    Dto gwdct = this.baseMapper.getGwdc_jt(queryDto);
                    Dto gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_jt",queryDto);
                    if (gwdct != null) {
                        resultDto.put("GWDC020", gwdct.getString("GWDC020"));
                        resultDto.put("GWDC021", gwdct.getString("GWDC021"));
                        resultDto.put("GWDC025", gwdct.getString("GWDC025"));
                        resultDto.put("GWDC026", gwdct.getString("GWDC026"));
                    }
                } else {
                    resultDto = this.baseMapper.getData2(queryDto);
                }
            }
            listData.get(i).put("GWCS003_SUM", resultDto.getBigDecimal("GWCS003_SUM"));
            listData.get(i).put("GWCS005_SUM", resultDto.getBigDecimal("GWCS005_SUM"));
            listData.get(i).put("GWDC020", resultDto.getString("GWDC020"));
            listData.get(i).put("GWDC021", resultDto.getString("GWDC021"));
            listData.get(i).put("GWDC025", resultDto.getString("GWDC025"));
            listData.get(i).put("GWDC026", resultDto.getString("GWDC026"));
            if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                if (resultDto.getString("GWDC025") == null || resultDto.getString("GWDC025").isEmpty()) {//最大赠品金额为空
                    listData.get(i).put("GWDC025_MAX", "");
                    BigDecimal num = resultDto.getBigDecimal("GWCS003_SUM").divide(resultDto.getBigDecimal("GWDC020")).setScale(0, RoundingMode.DOWN);
                    listData.get(i).put("GWDC026_MAX", num.multiply(resultDto.getBigDecimal("GWDC026")));
                } else {
                    listData.get(i).put("GWDC026_MAX", "");
                    BigDecimal num = resultDto.getBigDecimal("GWCS003_SUM").divide(resultDto.getBigDecimal("GWDC020")).setScale(0, RoundingMode.DOWN);
                    ;
                    listData.get(i).put("GWDC025_MAX", num.multiply(resultDto.getBigDecimal("GWDC025")));
                }
            } else if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                if (resultDto.getString("GWDC025") == null || resultDto.getString("GWDC025").isEmpty()) {//最大赠品金额为空
                    listData.get(i).put("GWDC025_MAX", "");
                    BigDecimal num = resultDto.getBigDecimal("GWCS005_SUM").divide(resultDto.getBigDecimal("GWDC021")).setScale(0, RoundingMode.DOWN);
                    ;
                    listData.get(i).put("GWDC026_MAX", num.multiply(resultDto.getBigDecimal("GWDC026")));
                } else {
                    listData.get(i).put("GWDC026_MAX", "");
                    BigDecimal num = resultDto.getBigDecimal("GWCS005_SUM").divide(resultDto.getBigDecimal("GWDC021")).setScale(0, RoundingMode.DOWN);
                    ;
                    listData.get(i).put("GWDC025_MAX", num.multiply(resultDto.getBigDecimal("GWDC025")));
                }
            }
        }
        return listData;
    }

    /**
     * 根据当前单号获取赠品可开窗的活动信息
     */
    @Transactional(readOnly = true)
    public List<Dto> getGwdcOpenData(Page<Object> page, Dto dto) {
        dto.put("GWCSENT", dto.getString("ENT"));
        //获取该订单下符合满减满赠的活动数据
        List<Dto> listGwcs109 = this.baseMapper.getGwcs109(dto);
        List<Dto> listData = new ArrayList<>();
        for (int k = 0; k < listGwcs109.size(); k++) {
            if (listGwcs109.get(k).getString("GWCS111").equals("1")) {
                listData = this.baseMapper.getGwcsData(page, dto);
            } else {
                listData = this.baseMapper.getGwcsData_(page, dto);
            }
        }

        //循环活动信息获取商品信息、订单总数量/金额、起订数量/金额、最大赠品数量/金额
        for (int i = 0; i < listData.size(); i++) {
            Dto queryDto = dto;
            Dto resultDto = Dtos.newDto();
            queryDto.put("GWCS109", listData.get(i).getString("GWCS109"));
            if (listData.get(i).getString("GWDC007").equals("1")) {//渠道
//                Dto gwak = this.baseMapper.getGwak(dto);
                Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
                queryDto.put("GWDC016", gwak.getString("GWAK010"));
            } else {//渠道商
                queryDto.put("GWDC016", dto.getString("GWCS010"));
            }
            if (listData.get(i).getString("GWDC004").equals("1")) {//组合
                if (listData.get(i).getString("GWDC022").equals("Y")) {
                    resultDto = this.baseMapper.getData1_jt(queryDto);
                    if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                        queryDto.put("sum_gwcs005", resultDto.getString("GWCS005_SUM"));
                    }
                    if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                        queryDto.put("sum_gwcs003", resultDto.getString("GWCS003_SUM"));
                    }

//                    Dto gwdct = this.baseMapper.getGwdct(queryDto);
                    Dto gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdct",queryDto);
                    if (gwdct != null) {
                        resultDto.put("GWDC020", gwdct.getString("GWDC020"));
                        resultDto.put("GWDC021", gwdct.getString("GWDC021"));
                        resultDto.put("GWDC025", gwdct.getString("GWDC025"));
                        resultDto.put("GWDC026", gwdct.getString("GWDC026"));
                    }
                } else {
                    resultDto = this.baseMapper.getData1(queryDto);
                }
            } else {//非组合

                queryDto.put("GWCS001", listData.get(i).getString("GWCS001"));
                if (listData.get(i).getString("GWDC022").equals("Y")) {
                    resultDto = this.baseMapper.getData2_jt(queryDto);
                    if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                        queryDto.put("sum_gwcs005", resultDto.getString("GWCS005_SUM"));
                    }
                    if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                        queryDto.put("sum_gwcs003", resultDto.getString("GWCS003_SUM"));
                    }

//                    Dto gwdct = this.baseMapper.getGwdc_jt(queryDto);
                    Dto gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_jt",queryDto);
                    if (gwdct != null) {
                        resultDto.put("GWDC020", gwdct.getString("GWDC020"));
                        resultDto.put("GWDC021", gwdct.getString("GWDC021"));
                        resultDto.put("GWDC025", gwdct.getString("GWDC025"));
                        resultDto.put("GWDC026", gwdct.getString("GWDC026"));
                    }
                } else {
                    resultDto = this.baseMapper.getData2(queryDto);
                }
            }
            if (resultDto != null) {
                listData.get(i).put("GWCS001", resultDto.getString("GWCS001"));
                listData.get(i).put("GWAML003", resultDto.getString("GWAML003"));
                listData.get(i).put("GWAML004", resultDto.getString("GWAML004"));
                listData.get(i).put("GWCS003_SUM", resultDto.getString("GWCS003_SUM"));
                listData.get(i).put("GWCS005_SUM", resultDto.getString("GWCS005_SUM"));
                listData.get(i).put("GWDC020", resultDto.getString("GWDC020"));
                listData.get(i).put("GWDC021", resultDto.getString("GWDC021"));
                listData.get(i).put("GWDC025", resultDto.getString("GWDC025"));
                listData.get(i).put("GWDC026", resultDto.getString("GWDC026"));
//                listData.get(i).put("GWDC005",resultDto.getString("GWDC005"));
                if (listData.get(i).getString("GWDC005").equals("4")) {//满量
                    if (resultDto.getString("GWDC025").isEmpty()) {//最大赠品金额为空
                        listData.get(i).put("GWDC025_MAX", "");
                        BigDecimal num = resultDto.getBigDecimal("GWCS003_SUM").divide(resultDto.getBigDecimal("GWDC020")).setScale(0, RoundingMode.DOWN);
                        ;
                        listData.get(i).put("GWDC026_MAX", num.multiply(resultDto.getBigDecimal("GWDC026")));
                    } else {
                        listData.get(i).put("GWDC026_MAX", "");
                        BigDecimal num = resultDto.getBigDecimal("GWCS003_SUM").divide(resultDto.getBigDecimal("GWDC020")).setScale(0, RoundingMode.DOWN);
                        ;
                        listData.get(i).put("GWDC025_MAX", num.multiply(resultDto.getBigDecimal("GWDC025")));
                    }
                } else if (listData.get(i).getString("GWDC005").equals("3")) {//满额
                    if (resultDto.getString("GWDC025").isEmpty()) {//最大赠品金额为空
                        listData.get(i).put("GWDC025_MAX", "");
                        BigDecimal num = resultDto.getBigDecimal("GWCS005_SUM").divide(resultDto.getBigDecimal("GWDC021")).setScale(0, RoundingMode.DOWN);
                        ;
                        listData.get(i).put("GWDC026_MAX", num.multiply(resultDto.getBigDecimal("GWDC026")));
                    } else {
                        listData.get(i).put("GWDC026_MAX", "");
                        BigDecimal num = resultDto.getBigDecimal("GWCS005_SUM").divide(resultDto.getBigDecimal("GWDC021")).setScale(0, RoundingMode.DOWN);
                        ;
                        listData.get(i).put("GWDC025_MAX", num.multiply(resultDto.getBigDecimal("GWDC025")));
                    }
                }
            }

        }
        return listData;
    }

    /**
     * 获取赠品信息开窗
     */
    @Transactional(readOnly = true)
    public List<Dto> getGwdczOpenData(Page<Object> page, Dto dto) {
        //获取活动数据
        Dto gwdcData = this.baseMapper.getGwdc004(dto);
        Dto queryDto = dto;
        //判断对象类型
        List<Dto> listData = new ArrayList<>();
        if (gwdcData.getString("GWDC007").equals("1")) {//渠道
//            Dto gwak = this.baseMapper.getGwak(dto);
            Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
            queryDto.put("GWDC016", gwak.getString("GWAK010"));
        } else {
            queryDto.put("GWDC016", dto.getString("GWCS010"));
        }
        if (gwdcData.getString("GWDC004").equals("1")) {//组合
            listData = this.baseMapper.getGwdcz1(page, queryDto);
        } else {//非组合
            listData = this.baseMapper.getGwdcz2(page, queryDto);
        }
        return listData;
    }

    @Transactional
    public void addGwcszData(List<GwcszT> gwcszTList, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String GWCSZDOCNO = dto.getString("GWCSDOCNO");
        GwcszT gwcszT = new GwcszT();
        UpdateWrapper<GwcszT> gwcszTUpdateWrapper = new UpdateWrapper<>();
        gwcszTUpdateWrapper.eq("GWCSZENT", shiroUser.getDBC());
        gwcszTUpdateWrapper.eq("GWCSZDOCNO", GWCSZDOCNO);
        gwcszT.delete(gwcszTUpdateWrapper);
        //增加赠品明细
        for (GwcszT gwcszT1 : gwcszTList) {
            gwcszT1.setGWCSZENT(shiroUser.getDBC());
            gwcszT1.insert();
        }
    }

    @Transactional(readOnly = true)
    public Dto getGwcsz005_006(Dto dto) {
        Dto gwdct = null;
//        Dto gwdc = this.baseMapper.getGwdc(dto);
        Dto gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc",dto);
        if (gwdc == null) {
//            Dto gwak = this.baseMapper.getGwak(dto);
            Dto gwak = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwak",dto);
            if (!(gwak == null)) {
                dto.put("GWCS010", gwak.getString("GWAK010"));
            }
//            gwdc = this.baseMapper.getGwdc(dto);
            gwdc = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc",dto);
        }
        //组合+阶梯定价
        if (gwdc.getString("GWDC022").equals("Y") && gwdc.getString("GWDC004").equals("1")) {
//            gwdct = this.baseMapper.getGwdct(dto);
            gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdct",dto);
        }
        if (gwdct == null) {
//            gwdct = this.baseMapper.getGwdct_ml(dto);//组合满量
            gwdct = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdct_m1",dto);
        }
        if (gwdct != null) {
            gwdc.put("GWDC020", gwdct.getBigDecimal("GWDC020"));
            gwdc.put("GWDC021", gwdct.getBigDecimal("GWDC021"));
            gwdc.put("GWDC025", gwdct.getBigDecimal("GWDC025"));
            gwdc.put("GWDC026", gwdct.getBigDecimal("GWDC026"));
        }
        Dto gwcsz = this.baseMapper.getGwcsz005_006(dto);
        if (gwcsz != null) {
            gwdc.put("GWCSZ005", gwcsz.getBigDecimal("GWCSZ005"));
            gwdc.put("GWCSZ006", gwcsz.getBigDecimal("GWCSZ006"));
        }
        gwdc.put("GWDC020", gwdc.getBigDecimal("GWDC020"));
        gwdc.put("GWDC021", gwdc.getBigDecimal("GWDC021"));
        gwdc.put("GWDC025", gwdc.getBigDecimal("GWDC025"));
        gwdc.put("GWDC026", gwdc.getBigDecimal("GWDC026"));
        return gwdc;
    }

    @Transactional(readOnly = true)
    public Dto getGwcs111(Dto dto) {
        Dto gwcs = this.baseMapper.getGwcs111(dto);
        return gwcs;
    }

    @Transactional(readOnly = true)
    public Dto checkGwcs117(Dto dto) {
        Dto outDto = Dtos.newDto();
        dto.put("flag", true);
        if (dto.getString("GWCS117").equals("1") || dto.getString("GWCS117").equals("2")) {
            outDto = this.baseMapper.checkGwcs117(dto);
        } else {
            dto.put("flag", false);
            outDto = this.baseMapper.checkGwcs117(dto);
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public String getMaxTc002(Dto str) {
        String maxTc002 = this.baseMapper.getMaxTc002(str);
        return maxTc002;
    }

    public Dto getAddress(Dto pDto) {
//        Dto adrDto = this.baseMapper.getAddress(pDto);
        Dto adrDto = InvokeUtil.invokeBeanMethod("gwcsTMapper,getAddress",pDto);
        return adrDto;
    }

    public Dto getYewu(Dto pDto) {
        Dto adrDto = this.baseMapper.getYewu(pDto);
        return adrDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMaxSaleProduct(Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWCSENT", shiroUser.getDBC());
//        List<Dto> result = this.baseMapper.getMaxSaleProduct(dto);
        List<Dto> result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getMaxSaleProduct",dto);
        return result;
    }

    @Transactional(readOnly = true)
    public List<Dto> getOnSaleProduct(Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWCSENT", shiroUser.getDBC());
//        List<Dto> result = this.baseMapper.getOnSaleProduct(dto);
        List<Dto> result = InvokeUtil.invokeBeanMethod("gwcsTMapper,getOnSaleProduct",dto);
        return result;
    }

    @Transactional(readOnly = true)
    public Dto getMB(Dto dto) {
        Dto mbDto = this.baseMapper.getMB(dto);
        return mbDto;
    }

    @Transactional(readOnly = true)
    public Dto getTD032(Dto dto) {
        Dto mbDto = this.baseMapper.getTD032(dto);
        return mbDto;
    }

    @Transactional(readOnly = true)
    public boolean getGwdfCount(Dto dto) {
        Boolean flag = false;
        Dto result = this.baseMapper.getGwdfCount(dto);
        if (result.getBigDecimal("COUNT").compareTo(new BigDecimal(0)) == 1) {
            flag = true;
        }
        return flag;
    }

    @Transactional(readOnly = true)
    public boolean getCopthCount(List<Dto> dto) {
        Boolean flag = false;
        for (Dto qDto : dto) {
            Dto result = this.baseMapper.getCopthCount(qDto);
            if (result.getBigDecimal("COUNT").compareTo(new BigDecimal(0)) == 1) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    @Transactional(readOnly = true)
    public Dto getPriceSingel(Dto dto) {
        Dto rt = Dtos.newDto();
        Dto tDto = this.baseMapper.getGwdc004_007(dto);
        if (!tDto.getString("GWDC005").equals("3")) {
//            rt = this.baseMapper.getGwdc_jieti(dto);
            rt = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_jieti",dto);
        } else {
            dto.put("GWDC021_", dto.getBigDecimal("GWCS003").multiply(dto.getBigDecimal("GWCS115")));
//            rt = this.baseMapper.getGwdc_jieti_3(dto);
            rt = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdc_jieti_3",dto);
        }
        /*if(!gwdc.isEmpty()){
            gwdc = this.baseMapper.getGwdb016(dto);
        }*/

        return rt;
    }

    @Transactional
    @Override
    public void updateGWCS113(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
        gwcsTUpdateWrapper.eq("GWCSENT", shiroUser.getDBC());
        gwcsTUpdateWrapper.eq("GWCSDOCNO", dto.getString("GWCSDOCNO"));
        GwcsT gwcsT = new GwcsT();
        gwcsT.setGWCS113(dto.getString("GWCS113"));
        gwcsT.setGWCS112(dto.getString("GWCS112"));
        gwcsT.update(gwcsTUpdateWrapper);
    }

    public void createTable(String tableName) throws IllegalAccessException, InvocationTargetException {
        try {
            switch(AppCxt.getParam("ERP")){
                case "T100":
                    this.t100Mapper.createTable(tableName);
                    break;
                case "GP":
                    this.baseMapper.createTable(tableName);
                    break;
                case "E10":
                    this.baseMapper.createTable(tableName);
                    break;
                case "YF":
                    this.baseMapper.createTable(tableName);
                    break;
                default:
                    this.baseMapper.createTable(tableName);
                    break;
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Transactional
    public void insertLS(List<GwcsT> list, String tableName) {
        for (GwcsT gwcsT : list) {
            if (gwcsT.getGWCS117() != null && gwcsT.getGWCS113() != null && gwcsT.getGWCS016() != null
                    && gwcsT.getGWCSDOCDATE() != null && gwcsT.getGWCS003() != null && gwcsT.getGWCS010() != null) {
                Dto result = this.isExsit(gwcsT);
                if (result.getBoolean("flag")) {
                    if (gwcsT.getGWCS016().compareTo(gwcsT.getGWCSDOCDATE()) > 0) {//订单日期在预交日期之前
                        Dto insert = Dtos.newDto();
                        insert.put("LS001", gwcsT.getGWCS117()); //订单类型
                        insert.put("LS002", gwcsT.getGWCS109()); //活动编号
                        insert.put("LS003", gwcsT.getGWCS113()); //发货仓库
                        insert.put("LS004", gwcsT.getGWCS016()); //预计交货期
                        insert.put("LS005", gwcsT.getGWCSDOCDATE()); //订单日期
                        insert.put("LS006", gwcsT.getGWCS003()); //订单数量
                        insert.put("LS007", gwcsT.getGWCS001()); //产品
                        insert.put("LS008", gwcsT.getGWCS010()); //客户编号
                        if (gwcsT.getGWCS109() != null) {
                            insert.put("LS009", result.getString("LS009")); //活动类型
                            insert.put("LS010", result.getString("LS010")); //促销方式
                            insert.put("LS011", result.getString("LS011")); //促销方式
                        }
                        insert.put("tableName", tableName);
                        this.baseMapper.insertLS(insert);
                    }
                }
            }

        }
    }

    public List<Dto> getLs(Dto inDto) {
        List<Dto> lsDto = this.baseMapper.getLs(inDto);
        return lsDto;
    }

    public Dto isExsit(GwcsT gwcsT) {
        Dto dto = Dtos.newDto();
        Boolean flag = true;
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto queryDto = Dtos.newDto();
        queryDto.put("ENT", shiroUser.getDBC());
        queryDto.put("GWAK001", gwcsT.getGWCS010());
        queryDto.put("GWAM001", gwcsT.getGWCS001());
        queryDto.put("GWCSDOCDATE", gwcsT.getGWCSDOCDATE());
        queryDto.put("GWCS117", gwcsT.getGWCS117());
        //产品是否存在且有效
        Dto gwamDto = this.baseMapper.getGwamCount(queryDto);
        if (gwamDto == null) {
            flag = false;
            dto.put("flag", flag);
            return dto;
        }
        //客户是否存在且有效
        Dto gwakDto = this.baseMapper.getGwakCount(queryDto);
        if (gwakDto == null) {
            flag = false;
            dto.put("flag", flag);
            return dto;
        } else {
            //活动是否存在且有效
            if (gwcsT.getGWCS109() != null) {
                queryDto.put("GWDC001", gwcsT.getGWCS109());
                queryDto.put("GWAK010", gwakDto.getString("GWAK010"));
                Dto gwdcDto = this.baseMapper.getGwdcCount(queryDto);
                if (gwdcDto == null) {
                    flag = false;
                    dto.put("flag", flag);
                    return dto;
                } else {
                    dto.put("flag", flag);
                    dto.put("LS009", gwdcDto.getString("GWDC004"));
                    dto.put("LS010", gwdcDto.getString("GWDC005"));
                    dto.put("LS011", gwdcDto.getString("GWDC007"));
                    return dto;
                }
            }
            dto.put("flag", flag);
            return dto;
        }
    }

    public List<Dto> getLsByGroup(String tableName) {
        List<Dto> lsDto = this.baseMapper.getLsByGroup(tableName);
        return lsDto;
    }

    //获取产品结算价
    @Transactional(readOnly = true)
    public Dto getGwdc023(Dto dto) {
//        Dto outDto = this.baseMapper.getGwdb1(dto);
        Dto outDto = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdb1",dto);
        if (outDto == null) {
//            outDto = this.baseMapper.getGwdb2(dto);
            outDto = InvokeUtil.invokeBeanMethod("gwcsTMapper,getGwdb2",dto);
        }
        return outDto;
    }

    public BigDecimal getMaxGwcsseq(String GWCSDOCNO) {
        Dto inDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("GWCSDOCNO", GWCSDOCNO);
        BigDecimal gwcsseq_max = new BigDecimal(0);
        Dto result = this.baseMapper.getMaxGwcsseq(inDto);
        if (result != null) {
            gwcsseq_max = result.getBigDecimal("GWCSSEQ_MAX");
        }
        return gwcsseq_max;
    }

    @Transactional
    public void insertGwcs(String tableName, List<Dto> ls_list) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (int i = 0; i < ls_list.size(); i++) {
            Date date = new Date();
            String GWCSDOCNO = "ZS" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss);
            Dto lsDto = ls_list.get(i);
            lsDto.put("tableName", tableName);
            //查询客户相关信息、汇率等
            Dto gwakQ = Dtos.newDto();
            gwakQ.put("GWAK001", lsDto.getString("LS008"));
            gwakQ.put("ENT", shiroUser.getDBC());
            Dto GWCS034 = this.getGwcs034(gwakQ);
            Dto GWCS035 = this.getGwcs035(gwakQ);
            Dto gwakDto = this.baseMapper.getGwakCount(gwakQ);
            List<Dto> t1 = this.getLs(lsDto);
            //组合+裸价
            lsDto.put("LS009", '1');
            lsDto.put("LS010", '1');
            lsDto.put("ENT", shiroUser.getDBC());
            List<Dto> huodongType1 = this.baseMapper.huodongType1(lsDto);
            if (huodongType1 != null) {
                for (int j = 0; j < huodongType1.size(); j++) {
                    //获取该订单最大seq
                    BigDecimal gwcsseq_max = this.getMaxGwcsseq(GWCSDOCNO);
                    String gwdc001 = huodongType1.get(j).getString("LS002");//活动编号
                    String gwdc007 = huodongType1.get(j).getString("LS011");//对象类型
                    if (("1").equals(gwdc007)) {
                        lsDto.put("GWAK", gwakDto.getString("GWAK010"));
                        lsDto.put("LS011", gwdc007);
                    } else {
                        lsDto.put("LS011", gwdc007);
                    }
                    //判断是否满足活动
                    //该活动所有商品数量、金额求和
                    lsDto.put("LS002", gwdc001);
//                    Dto checkHuoDong1 = this.baseMapper.checkHuoDong1(lsDto);
                    Dto checkHuoDong1 = InvokeUtil.invokeBeanMethod("gwcsTMapper,checkHuoDong1",lsDto);
                    int flag20 = checkHuoDong1.getBigDecimal("GWDC020").compareTo(new BigDecimal(0));
                    int flag21 = checkHuoDong1.getBigDecimal("GWDC021").compareTo(checkHuoDong1.getBigDecimal("GWDC024_SUM"));
                    if (flag20 == 0 && (flag21 == -1 || flag21 == 0)) {
                        //满足起订金额
//                        List<Dto> HuoDong_list = this.baseMapper.getHuoDongList(lsDto);
                        List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList",lsDto);
                        for (int a = 0; a < HuoDong_list.size(); a++) {
                            Dto huodong = HuoDong_list.get(a);
                            GwcsT gwcsT = new GwcsT();
                            gwcsT.setGWCSENT(shiroUser.getDBC());
                            gwcsT.setGWCSDOCNO(GWCSDOCNO);
                            gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                            gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                            gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                            gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                            gwcsT.setGWCS004(huodong.getBigDecimal("GWDC024"));//原币单价
                            gwcsT.setGWCS005(huodong.getBigDecimal("jine"));//金额
                            gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                            gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                            gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                            gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                            gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                            gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                            gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                            gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(huodong.getBigDecimal("jine")));//折扣后金额
                            gwcsT.setGWCS037(huodong.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                            gwcsT.setGWCS038(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS039(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                            //原币税额
                            BigDecimal GWCS040 = huodong.getBigDecimal("jine").subtract(
                                    huodong.getBigDecimal("jine").divide(
                                            new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                            );
                            gwcsT.setGWCS040(GWCS040);
                            gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                            gwcsT.setGWCS110(huodong.getString("GWDC005"));//促销方式
                            gwcsT.setGWCS111(huodong.getString("GWDC004"));//促销单据类型
                            gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                            gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                            gwcsT.setGWCS115(huodong.getBigDecimal("GWDC023"));//结算价
                            gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                            gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                            gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                            if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                gwcsT.setGWCS119("2");//新增类型 铺货新增
                            } else {
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                            }
                            gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSACTI("T");
                            gwcsT.setGWCSUSER(shiroUser.getID());
                            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                            gwcsT.setGWCSDATE(LocalDate.now());
                            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                            gwcsT.setGWCSORIU(shiroUser.getID());

                            gwcsT.insert();
                        }
                    }
                    int flag20_1 = checkHuoDong1.getBigDecimal("GWDC021").compareTo(new BigDecimal(0));
                    int flag21_1 = checkHuoDong1.getBigDecimal("GWDC020").compareTo(checkHuoDong1.getBigDecimal("LS006_SUM"));
                    if (flag20_1 == 0 && (flag21_1 == -1 || flag21_1 == 0)) {
                        //满足起订数量
//                        List<Dto> HuoDong_list = this.baseMapper.getHuoDongList(lsDto);
                        List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList",lsDto);
                        for (int a = 0; a < HuoDong_list.size(); a++) {
                            Dto huodong = HuoDong_list.get(a);
                            GwcsT gwcsT = new GwcsT();
                            gwcsT.setGWCSENT(shiroUser.getDBC());
                            gwcsT.setGWCSDOCNO(GWCSDOCNO);
                            gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                            gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                            gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                            gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                            gwcsT.setGWCS004(huodong.getBigDecimal("GWDC024"));//原币单价
                            gwcsT.setGWCS005(huodong.getBigDecimal("jine"));//金额
                            gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                            gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                            gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                            gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                            gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                            gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                            gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                            gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(huodong.getBigDecimal("jine")));//折扣后金额
                            gwcsT.setGWCS037(huodong.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                            gwcsT.setGWCS038(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS039(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                            //原币税额
                            BigDecimal GWCS040 = huodong.getBigDecimal("jine").subtract(
                                    huodong.getBigDecimal("jine").divide(
                                            new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                            );
                            gwcsT.setGWCS040(GWCS040);
                            gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                            gwcsT.setGWCS110(huodong.getString("GWDC005"));//促销方式
                            gwcsT.setGWCS111(huodong.getString("GWDC004"));//促销单据类型
                            gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                            gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                            gwcsT.setGWCS115(huodong.getBigDecimal("GWDC023"));//结算价
                            gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                            gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                            gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                            if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                gwcsT.setGWCS119("2");//新增类型 铺货新增
                            } else {
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                            }
                            gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSACTI("T");
                            gwcsT.setGWCSUSER(shiroUser.getID());
                            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                            gwcsT.setGWCSDATE(LocalDate.now());
                            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                            gwcsT.setGWCSORIU(shiroUser.getID());

                            gwcsT.insert();
                        }
                    }
                    continue;
                }
            }
            //组合+特价
            lsDto.put("LS009", '1');
            lsDto.put("LS010", '2');
            lsDto.put("ENT", shiroUser.getDBC());
            List<Dto> huodongType2 = this.baseMapper.huodongType1(lsDto);
            if (huodongType2 != null) {
                for (int j = 0; j < huodongType2.size(); j++) {
                    //获取最大编号
                    BigDecimal gwcsseq_max = this.getMaxGwcsseq(GWCSDOCNO);
                    String gwdc001 = huodongType2.get(j).getString("LS002");//活动编号
                    String gwdc007 = huodongType1.get(j).getString("LS011");//对象类型
                    if (("1").equals(gwdc007)) {
                        lsDto.put("GWAK", gwakDto.getString("GWAK010"));
                        lsDto.put("LS011", gwdc007);
                    } else {
                        lsDto.put("LS011", gwdc007);
                    }
                    //判断是否满足活动
                    //该活动所有商品数量、金额求和
                    lsDto.put("LS002", gwdc001);
//                    Dto checkHuoDong1 = this.baseMapper.checkHuoDong1(lsDto);
                    Dto checkHuoDong1 = InvokeUtil.invokeBeanMethod("gwcsTMapper,checkHuoDong1",lsDto);
                    int flag21_1 = checkHuoDong1.getBigDecimal("GWDC020").compareTo(checkHuoDong1.getBigDecimal("LS006_SUM"));
                    if (flag21_1 == -1 || flag21_1 == 0) {
                        //满足起订数量
//                        List<Dto> HuoDong_list = this.baseMapper.getHuoDongList(lsDto);
                        List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList",lsDto);
                        for (int a = 0; a < HuoDong_list.size(); a++) {
                            Dto huodong = HuoDong_list.get(a);
                            GwcsT gwcsT = new GwcsT();
                            gwcsT.setGWCSENT(shiroUser.getDBC());
                            gwcsT.setGWCSDOCNO(GWCSDOCNO);
                            gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                            gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                            gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                            gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                            gwcsT.setGWCS004(huodong.getBigDecimal("GWDC024"));//原币单价
                            gwcsT.setGWCS005(huodong.getBigDecimal("jine"));//金额
                            gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                            gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                            gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                            gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                            gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                            gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                            gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                            gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(huodong.getBigDecimal("jine")));//折扣后金额
                            gwcsT.setGWCS037(huodong.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                            gwcsT.setGWCS038(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS039(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                            //原币税额
                            BigDecimal GWCS040 = huodong.getBigDecimal("jine").subtract(
                                    huodong.getBigDecimal("jine").divide(
                                            new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                            );
                            gwcsT.setGWCS040(GWCS040);
                            gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                            gwcsT.setGWCS110(huodong.getString("GWDC005"));//促销方式
                            gwcsT.setGWCS111(huodong.getString("GWDC004"));//促销单据类型
                            gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                            gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                            gwcsT.setGWCS115(huodong.getBigDecimal("GWDC023"));//结算价
                            gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                            gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                            gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                            if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                gwcsT.setGWCS119("2");//新增类型 铺货新增
                            } else {
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                            }
                            gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSACTI("T");
                            gwcsT.setGWCSUSER(shiroUser.getID());
                            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                            gwcsT.setGWCSDATE(LocalDate.now());
                            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                            gwcsT.setGWCSORIU(shiroUser.getID());

                            gwcsT.insert();
                        }
                    }
                    continue;
                }
            }
            //非组合+裸价
            lsDto.put("LS009", '2');
            lsDto.put("LS010", '1');
            lsDto.put("ENT", shiroUser.getDBC());
            List<Dto> huodongType3 = this.baseMapper.huodongType2(lsDto);
            if (huodongType3 != null) {
                for (int j = 0; j < huodongType3.size(); j++) {
                    //获取最大编号
                    BigDecimal gwcsseq_max = this.getMaxGwcsseq(GWCSDOCNO);
                    String gwdc001 = huodongType3.get(j).getString("LS002");//活动编号
                    String gwdc018 = huodongType3.get(j).getString("LS007");//产品编号
                    String gwdc007 = huodongType3.get(j).getString("LS011");//对象类型
                    if (("1").equals(gwdc007)) {
                        lsDto.put("GWAK", gwakDto.getString("GWAK010"));
                        lsDto.put("LS011", gwdc007);
                    } else {
                        lsDto.put("LS011", gwdc007);
                    }
                    //判断是否满足活动
                    //该活动及商品数量、金额求和
                    lsDto.put("LS002", gwdc001);
                    lsDto.put("LS007", gwdc018);
//                    Dto checkHuoDong2 = this.baseMapper.checkHuoDong2(lsDto);
                    Dto checkHuoDong2 = InvokeUtil.invokeBeanMethod2("gwcsTMapper,checkHuoDong2",lsDto);
                    int flag20 = checkHuoDong2.getBigDecimal("GWDC020").compareTo(new BigDecimal(0));
                    int flag21 = checkHuoDong2.getBigDecimal("GWDC021").compareTo(checkHuoDong2.getBigDecimal("GWDC024_SUM"));
                    if (flag20 == 0 && (flag21 == -1 || flag21 == 0)) {
                        //满足起订金额
//                        List<Dto> HuoDong_list = this.baseMapper.getHuoDongList2(lsDto);
                        List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList2",lsDto);
                        for (int a = 0; a < HuoDong_list.size(); a++) {
                            Dto huodong = HuoDong_list.get(a);
                            GwcsT gwcsT = new GwcsT();
                            gwcsT.setGWCSENT(shiroUser.getDBC());
                            gwcsT.setGWCSDOCNO(GWCSDOCNO);
                            gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                            gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                            gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                            gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                            gwcsT.setGWCS004(huodong.getBigDecimal("GWDC024"));//原币单价
                            gwcsT.setGWCS005(huodong.getBigDecimal("jine"));//金额
                            gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                            gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                            gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                            gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                            gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                            gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                            gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                            gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(huodong.getBigDecimal("jine")));//折扣后金额
                            gwcsT.setGWCS037(huodong.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                            gwcsT.setGWCS038(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS039(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                            //原币税额
                            BigDecimal GWCS040 = huodong.getBigDecimal("jine").subtract(
                                    huodong.getBigDecimal("jine").divide(
                                            new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                            );
                            gwcsT.setGWCS040(GWCS040);
                            gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                            gwcsT.setGWCS110(huodong.getString("GWDC005"));//促销方式
                            gwcsT.setGWCS111(huodong.getString("GWDC004"));//促销单据类型
                            gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                            gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                            gwcsT.setGWCS115(huodong.getBigDecimal("GWDC023"));//结算价
                            gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                            gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                            gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                            if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                gwcsT.setGWCS119("2");//新增类型 铺货新增
                            } else {
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                            }
                            gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSACTI("T");
                            gwcsT.setGWCSUSER(shiroUser.getID());
                            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                            gwcsT.setGWCSDATE(LocalDate.now());
                            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                            gwcsT.setGWCSORIU(shiroUser.getID());

                            gwcsT.insert();
                        }
                    }
                    int flag20_1 = checkHuoDong2.getBigDecimal("GWDC021").compareTo(new BigDecimal(0));
                    int flag21_1 = checkHuoDong2.getBigDecimal("GWDC020").compareTo(checkHuoDong2.getBigDecimal("LS006_SUM"));
                    if (flag20_1 == 0 && (flag21_1 == -1 || flag21_1 == 0)) {
                        //满足起订数量
//                        List<Dto> HuoDong_list = this.baseMapper.getHuoDongList2(lsDto);
                        List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList2",lsDto);
                        for (int a = 0; a < HuoDong_list.size(); a++) {
                            Dto huodong = HuoDong_list.get(a);
                            GwcsT gwcsT = new GwcsT();
                            gwcsT.setGWCSENT(shiroUser.getDBC());
                            gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                            gwcsT.setGWCSDOCNO(GWCSDOCNO);
                            gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                            gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                            gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                            gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                            gwcsT.setGWCS004(huodong.getBigDecimal("GWDC024"));//原币单价
                            gwcsT.setGWCS005(huodong.getBigDecimal("jine"));//金额
                            gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                            gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                            gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                            gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                            gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                            gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                            gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                            gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(huodong.getBigDecimal("jine")));//折扣后金额
                            gwcsT.setGWCS037(huodong.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                            gwcsT.setGWCS038(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS039(huodong.getBigDecimal("jine").multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                            //原币税额
                            BigDecimal GWCS040 = huodong.getBigDecimal("jine").subtract(
                                    huodong.getBigDecimal("jine").divide(
                                            new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                            );
                            gwcsT.setGWCS040(GWCS040);
                            gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                            gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                            gwcsT.setGWCS110(huodong.getString("GWDC005"));//促销方式
                            gwcsT.setGWCS111(huodong.getString("GWDC004"));//促销单据类型
                            gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                            gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                            gwcsT.setGWCS115(huodong.getBigDecimal("GWDC023"));//结算价
                            gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                            gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                            gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                            if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                gwcsT.setGWCS119("2");//新增类型 铺货新增
                            } else {
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                            }
                            gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                            gwcsT.setGWCSACTI("T");
                            gwcsT.setGWCSUSER(shiroUser.getID());
                            gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                            gwcsT.setGWCSDATE(LocalDate.now());
                            gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                            gwcsT.setGWCSORIU(shiroUser.getID());

                            gwcsT.insert();
                        }
                    }
                    continue;
                }
                //非组合+特价
                lsDto.put("LS009", '2');
                lsDto.put("LS010", '2');
                lsDto.put("ENT", shiroUser.getDBC());
                List<Dto> huodongType4 = this.baseMapper.huodongType2(lsDto);
                if (huodongType4 != null) {
                    for (int j = 0; j < huodongType4.size(); j++) {
                        //获取最大编号
                        BigDecimal gwcsseq_max = this.getMaxGwcsseq(GWCSDOCNO);
                        String gwdc001 = huodongType4.get(j).getString("LS002");//活动编号
                        String gwdc018 = huodongType4.get(j).getString("LS007");//产品编号
                        String gwdc007 = huodongType4.get(j).getString("LS011");//对象类型
                        if (("1").equals(gwdc007)) {
                            lsDto.put("GWAK", gwakDto.getString("GWAK010"));
                            lsDto.put("LS011", gwdc007);
                        } else {
                            lsDto.put("LS011", gwdc007);
                        }
                        //判断是否满足活动
                        //该活动及商品数量求和
                        lsDto.put("LS002", gwdc001);
                        lsDto.put("LS007", gwdc018);
                        Dto checkHuoDongSum = this.baseMapper.checkHuoDongSum(lsDto);//活动+产品求总数量
                        //获取该活动+产品最小起订数量
                        Dto minGwdc020 = this.baseMapper.getMinGwdc020(lsDto);
                        int flag = minGwdc020.getBigDecimal("GWDC020_MIN").compareTo(checkHuoDongSum.getBigDecimal("LS006_SUM"));
                        if (flag == 0 || flag == -1) {//小于等于
                            //满足起订数量  获取起订数量最近的价格
                            lsDto.put("NUM", minGwdc020.getBigDecimal("GWDC020_MIN"));
//                            Dto price = this.baseMapper.getPriceByCount(lsDto);
                            Dto price = InvokeUtil.invokeBeanMethod("gwcsTMapper,getPriceByCount",lsDto);
//                            List<Dto> HuoDong_list = this.baseMapper.getHuoDongList3(lsDto);
                            List<Dto> HuoDong_list = InvokeUtil.invokeBeanMethod("gwcsTMapper,getHuoDongList3",lsDto);
                            for (int a = 0; a < HuoDong_list.size(); a++) {
                                Dto huodong = HuoDong_list.get(a);
                                BigDecimal JINE = price.getBigDecimal("GWDC024").multiply(huodong.getBigDecimal("LS006"));
                                GwcsT gwcsT = new GwcsT();
                                gwcsT.setGWCSENT(shiroUser.getDBC());
                                gwcsT.setGWCSDOCNO(GWCSDOCNO);
                                gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(a + 1)));
                                gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                                gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                                gwcsT.setGWCS002(huodong.getString("GWDC019"));//销售单位
                                gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                                gwcsT.setGWCS004(price.getBigDecimal("GWDC024"));//原币单价
                                gwcsT.setGWCS005(JINE);//金额
                                gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                                gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                                gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                                gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                                gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                                gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                                gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                                gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(JINE));//折扣后金额
                                gwcsT.setGWCS037(price.getBigDecimal("GWDC024").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                                gwcsT.setGWCS038(JINE.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                                gwcsT.setGWCS039(JINE.multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                                //原币税额
                                BigDecimal GWCS040 = JINE.subtract(
                                        JINE.divide(
                                                new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                                );
                                gwcsT.setGWCS040(GWCS040);
                                gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                                gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                                gwcsT.setGWCS110(price.getString("GWDC005"));//促销方式
                                gwcsT.setGWCS111(price.getString("GWDC004"));//促销单据类型
                                gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                                gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                                gwcsT.setGWCS115(price.getBigDecimal("GWDC023"));//结算价
                                gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                                gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                                gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                                if (("5").equals(huodong.getString("LS001")) || ("6").equals(huodong.getString("LS001"))) {
                                    gwcsT.setGWCS119("2");//新增类型 铺货新增
                                } else {
                                    gwcsT.setGWCS119("1");//新增类型 正常新增
                                }
                                gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                                gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                                gwcsT.setGWCSACTI("T");
                                gwcsT.setGWCSUSER(shiroUser.getID());
                                gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                                gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                                gwcsT.setGWCSDATE(LocalDate.now());
                                gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                                gwcsT.setGWCSORIU(shiroUser.getID());

                                gwcsT.insert();
                            }
                        }
                        continue;
                    }

                    lsDto.put("LANG", LangUtils.getLang(shiroUser.getLANG()));
                    List<Dto> wuhuodong = this.baseMapper.getWuhuodong(lsDto);
                    //无活动
                    if (wuhuodong != null) {
                        for (int t = 0; t < wuhuodong.size(); t++) {
                            //获取最大编号
                            Dto huodong = wuhuodong.get(t);
                            BigDecimal gwcsseq_max = this.getMaxGwcsseq(GWCSDOCNO);
                            String gwdc018 = wuhuodong.get(t).getString("LS007");//产品编号
                            lsDto.put("GWAK", gwakDto.getString("GWAK010"));
                            lsDto.put("LS007", gwdc018);
                            Dto price = this.getGwdc023(lsDto);
                            if (price == null) {
                                continue;
                            } else {
                                BigDecimal JINE = price.getBigDecimal("GWDB016").multiply(huodong.getBigDecimal("LS006"));
                                GwcsT gwcsT = new GwcsT();
                                gwcsT.setGWCSENT(shiroUser.getDBC());
                                gwcsT.setGWCSDOCNO(GWCSDOCNO);
                                gwcsT.setGWCSSEQ(gwcsseq_max.add(new BigDecimal(t + 1)));
                                gwcsT.setGWCSDOCDATE(LocalDate.parse(huodong.getString("LS005"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                                gwcsT.setGWCS001(huodong.getString("LS007"));//产品
                                gwcsT.setGWCS002(price.getString("GWAM008"));//销售单位
                                gwcsT.setGWCS003(huodong.getBigDecimal("LS006"));//数量
                                gwcsT.setGWCS004(price.getBigDecimal("GWDB016"));//原币单价
                                gwcsT.setGWCS005(JINE);//金额
                                gwcsT.setGWCS006(GWCS034.getString("GWCS006"));//交易币别
                                gwcsT.setGWCS010(huodong.getString("LS008"));//客户
                                gwcsT.setGWCS016(LocalDate.parse(huodong.getString("LS004"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));//预计交货期
                                gwcsT.setGWCS032(huodong.getString("GWAK012"));//税种
                                gwcsT.setGWCS033(GWCS034.getString("GWCS033"));//汇率类型
                                gwcsT.setGWCS034(GWCS034.getBigDecimal("GWCS034"));//汇率
                                gwcsT.setGWCS035(GWCS035.getBigDecimal("GWCS035"));//折扣率
                                gwcsT.setGWCS036(GWCS035.getBigDecimal("GWCS035").multiply(JINE));//折扣后金额
                                gwcsT.setGWCS037(price.getBigDecimal("GWDB016").multiply(GWCS034.getBigDecimal("GWCS034")));//本币单价
                                gwcsT.setGWCS038(JINE.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                                gwcsT.setGWCS039(JINE.multiply(GWCS034.getBigDecimal("GWCS034")).multiply(GWCS035.getBigDecimal("GWCS035")));//本币折扣后金额
                                //原币税额
                                BigDecimal GWCS040 = JINE.subtract(
                                        JINE.divide(
                                                new BigDecimal(1).add(huodong.getBigDecimal("GWEA006").divide(new BigDecimal(100))), 2, ROUND_HALF_UP)
                                );
                                gwcsT.setGWCS040(GWCS040);
                                gwcsT.setGWCS041(GWCS040.multiply(GWCS034.getBigDecimal("GWCS034")));//本币金额
                                //gwcsT.setGWCS109(huodong.getString("LS002"));//活动编号
                                //gwcsT.setGWCS110(price.getString("GWDC005"));//促销方式
                                //gwcsT.setGWCS111(price.getString("GWDC004"));//促销单据类型
                                gwcsT.setGWCS112(huodong.getString("GWAS003"));//协议仓库类型
                                gwcsT.setGWCS113(huodong.getString("LS003"));//发货仓库
                                gwcsT.setGWCS115(price.getBigDecimal("GWDB016"));//结算价
                                gwcsT.setGWCS116(new BigDecimal(0));//标准结算价上浮
                                gwcsT.setGWCS117(huodong.getString("LS001"));//返利类型
                                gwcsT.setGWCS118(GWCS034.getString("GWCS118"));//拆包否
                                gwcsT.setGWCS119("1");//新增类型 正常新增
                                gwcsT.setGWCSPLANT(GWCS034.getString("GWCSPLANT"));
                                gwcsT.setGWCSLEGAL(GWCS034.getString("GWCSPLANT"));
                                gwcsT.setGWCSACTI("T");
                                gwcsT.setGWCSUSER(shiroUser.getID());
                                gwcsT.setGWCSGRUP(shiroUser.getORG_ID());
                                gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                                gwcsT.setGWCSDATE(LocalDate.now());
                                gwcsT.setGWCSORIG(shiroUser.getORG_ID());
                                gwcsT.setGWCSORIU(shiroUser.getID());

                                gwcsT.insert();
                            }
                        }
                    }
                }
            }
            GwcsT gwcsT = new GwcsT();
            gwcsT.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
            UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
            gwcsTUpdateWrapper.eq("GWCSENT", shiroUser.getDBC());
            gwcsTUpdateWrapper.eq("GWCSDOCNO", GWCSDOCNO);
            gwcsT.update(gwcsTUpdateWrapper);
        }
    }

    @Transactional
    public void add(List<GwcsT> gwcsTList, List<GwcszT> gwcszTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //int count = 1;
        for (GwcsT gwcsT : gwcsTList) {
            List<GwcszT> gwcszTList1 = new ArrayList<>();
            for (GwcszT gwcszT : gwcszTList) {
                if (gwcszT.getGWCSZ007().equals(gwcsT.getGWCS109())) {
                    gwcszTList1.add(gwcszT);
                }
            }
            Date date = new Date();
            String GWCSDOCNO = "ZS" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmssSSS);
            LocalDate gwcsdocdate = LocalDate.now();
            Dto inDto = Dtos.newDto();
            inDto.put("GWCQ010", gwcsT.getGWCS010());
            inDto.put("GWCQ109", gwcsT.getGWCS109());
            List<Dto> list = new ArrayList<>();
            if(gwcsT.getGWCS109()==null) gwcsT.setGWCS109("");
            if (gwcsT.getGWCS109().equals("")) {
//                list = gwcqTMapper.getGwcqList2(inDto);
                list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList2",inDto);
            } else {
//                list = gwcqTMapper.getGwcqList1(inDto);
                list = InvokeUtil.invokeBeanMethod("gwcqTMapper,getGwcqList1",inDto);
            }
            for (int i = 0; i < list.size(); i++) {
                GwcsT gwcs = gwcsT;
                gwcs.setGWCSENT(shiroUser.getDBC());
                gwcs.setGWCSDOCNO(GWCSDOCNO);
                gwcs.setGWCSSEQ(new BigDecimal(i + 1));
                gwcs.setGWCSDOCDATE(gwcsdocdate);
                gwcs.setGWCS001(list.get(i).getString("GWCQ001"));
                gwcs.setGWCS002(list.get(i).getString("GWCQ002"));
                gwcs.setGWCS003(list.get(i).getBigDecimal("GWCQ003"));
                gwcs.setGWCS004(list.get(i).getBigDecimal("GWCQ004"));
                gwcs.setGWCS005(list.get(i).getBigDecimal("GWCQ005"));
                gwcs.setGWCS006(list.get(i).getString("GWCQ006"));
                gwcs.setGWCS010(gwcsT.getGWCS010());
                gwcs.setGWCS014(shiroUser.getID());
                //gwcs.setGWCS016(gwcsdocdate);
                gwcs.setGWCS030(list.get(i).getString("GWCQDOCNO"));
                gwcs.setGWCS031(list.get(i).getBigDecimal("GWCQSEQ"));
                gwcs.setGWCSPLANT(list.get(i).getString("GWCQPLANT"));
                gwcs.setGWCSLEGAL(list.get(i).getString("GWCQLEGAL"));
                gwcs.setGWCSMLEGAL(list.get(i).getString("GWCQLEGAL"));
                gwcs.setGWCSACTI("N");
                Dto dto = Dtos.newDto();
                if(gwcsT.getGWCS010()==null){
                    dto.put("GWAK001", "");
                }else {
                    dto.put("GWAK001", gwcsT.getGWCS010());
                }
                Dto gwak1 = this.getGwcs034(dto);
                if (gwak1 != null) {
                    gwcs.setGWCS033(gwak1.getString("GWCS033"));
                    gwcs.setGWCS034(gwak1.getBigDecimal("GWCS034"));
                    gwcs.setGWCS118(gwak1.getString("GWCS118"));
                }
//                if(list.get(i).getString("GWCQ113").equals("")){购物车会带回     g
//                    gwcs.setGWCS115(new BigDecimal(0));
//                    gwcs.setGWCS035(new BigDecimal(1));
//                }else{
                gwcs.setGWCS115(list.get(i).getBigDecimal("GWCQ113"));
//                    gwcs.setGWCS035(list.get(i).getBigDecimal("GWCQ004").divide(gwcs.getGWCS115(),2,ROUND_HALF_DOWN));
                gwcs.setGWCS035(list.get(i).getBigDecimal("GWCQ103"));
//                }
                Dto gwaf = gwcqTMapper.getBizhong(gwcsT.getGWCS010());
                BigDecimal GWEA006 = new BigDecimal(0);
                if (!gwaf.getString("GWEA006").equals("")) {
                    GWEA006 = gwaf.getBigDecimal("GWEA006");
                }
                gwcs.setGWCS032(gwaf.getString("GWAK012"));
                gwcs.setGWCS036(gwcs.getGWCS005().multiply(gwcs.getGWCS035()));
                gwcs.setGWCS037(gwcs.getGWCS115().multiply(gwcs.getGWCS034()));
                gwcs.setGWCS038(gwcs.getGWCS005());
                gwcs.setGWCS039(gwcs.getGWCS038().multiply(gwcs.getGWCS035()));
                BigDecimal gwea006 = new BigDecimal(1).add(GWEA006.divide(new BigDecimal(100)));
                BigDecimal count = gwcs.getGWCS005().divide(gwea006,10,ROUND_HALF_UP);
                gwcs.setGWCS040(gwcs.getGWCS005().subtract(count));
                gwcs.setGWCS041(gwcs.getGWCS040().multiply(gwcs.getGWCS034()));
                gwcs.setGWCS109(gwcsT.getGWCS109());
                gwcs.setGWCS110(list.get(i).getString("GWDC005"));
                gwcs.setGWCS111(list.get(i).getString("GWDC004"));
                gwcs.setGWCS112(gwaf.getString("GWAS003"));
                gwcs.setGWCS113(gwaf.getString("GWAK058"));
                gwcs.setGWCS117(list.get(i).getString("GWCQ114"));
                gwcs.setGWCS119("1");
                gwcs.setGWCSUSER(shiroUser.getID());
                gwcs.setGWCSGRUP(shiroUser.getORG_ID());
                gwcs.setGWCSTIME(DateUtils.formatDate(date, "HH:mm:ss"));
                gwcs.setGWCSDATE(LocalDate.now());
                gwcs.setGWCSORIG(shiroUser.getORG_ID());
                gwcs.setGWCSORIU(shiroUser.getID());
                gwcs.insert();
                GwcqT gwcqT = new GwcqT();
                gwcqT.setGWCQDOCNO(list.get(i).getString("GWCQDOCNO"));
                gwcqT.setGWCQSEQ(list.get(i).getBigDecimal("GWCQSEQ"));
                gwcqT.setGWCQACTI("Z");
                UpdateWrapper<GwcqT> gwcqTUpdateWrapper = new UpdateWrapper<>();
                gwcqTUpdateWrapper.eq("GWCQDOCNO", list.get(i).getString("GWCQDOCNO"));
                gwcqTUpdateWrapper.eq("GWCQSEQ", list.get(i).getString("GWCQSEQ"));
                gwcqT.update(gwcqTUpdateWrapper);
            }

            for (GwcszT gwcszt : gwcszTList1) {
                gwcszt.setGWCSZDOCNO(GWCSDOCNO);
                gwcszt.setGWCSZENT(shiroUser.getDBC());
                gwcszt.insert();
            }
        }
    }

    public List<Dto> getAdmmf(Dto dto){
        return this.baseMapper.getAdmmf(dto);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwcsdocnoData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwcsDtos = this.baseMapper.getGwcsdocnoData(page, dto);
        return gwcsDtos;
    }

    public void dropTable(String tableName){
        this.t100Mapper.truncateTable(tableName);
        this.t100Mapper.dropTable(tableName);
    }
}
